package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.PaySplitVo;
import org.locker.domain.bo.PaySplitBo;
import org.locker.service.IPaySplitService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 微信分账
 *
 * @author winnie
 * @date 2024-01-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/paySplit")
public class PaySplitController extends BaseController {

    private final IPaySplitService paySplitService;

    /**
     * 查询微信分账列表
     */
    @SaCheckPermission("device:paySplit:list")
    @GetMapping("/list")
    public TableDataInfo<PaySplitVo> list(PaySplitBo bo, PageQuery pageQuery) {
        return paySplitService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微信分账列表
     */
    @SaCheckPermission("device:paySplit:export")
    @Log(title = "微信分账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PaySplitBo bo, HttpServletResponse response) {
        List<PaySplitVo> list = paySplitService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信分账", PaySplitVo.class, response);
    }

    /**
     * 获取微信分账详细信息
     *
     * @param paySplitId 主键
     */
    @SaCheckPermission("device:paySplit:query")
    @GetMapping("/{paySplitId}")
    public R<PaySplitVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long paySplitId) {
        return R.ok(paySplitService.queryById(paySplitId));
    }

    /**
     * 新增微信分账
     */
    @SaCheckPermission("device:paySplit:add")
    @Log(title = "微信分账", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PaySplitBo bo) {
        return toAjax(paySplitService.insertByBo(bo));
    }

    /**
     * 修改微信分账
     */
    @SaCheckPermission("device:paySplit:edit")
    @Log(title = "微信分账", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PaySplitBo bo) {
        return toAjax(paySplitService.updateByBo(bo));
    }

    /**
     * 删除微信分账
     *
     * @param paySplitIds 主键串
     */
    @SaCheckPermission("device:paySplit:remove")
    @Log(title = "微信分账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paySplitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] paySplitIds) {
        return toAjax(paySplitService.deleteWithValidByIds(List.of(paySplitIds), true));
    }
}
